<?php 
namespace app\mall\model;
use think\Model;
use think\Db;
class GoodsMaterial extends Model{
	protected $pk = 'id';
	protected $table = 'ys_goods_material';
	protected $field = true;        //自动忽略不存在的字段

	private function update_order($da){
        $name = $da['name'];            //商品名称
    	$res_num = $da['res_num'];      //
    	$material = $da['material_id'];
        $zigou = $da['is_zigou'];       //是否自购商品
    	$stock = $da['stock'];		//原料库存
        foreach($material as $k => $v){
            $data[$v]['id'] = $v;
            !isset($data[$v]['stock'])?$data[$v]['stock'] = $stock[$k]:$data[$v]['stock'];
            $data[$v]['stock'] -= $res_num[$k];
        }
    	return $data;
    }	
    /**
     * 更新总店原料库存
     * @return [type] [description]
     */
    public function save_order($data){
    	$da = $this->update_order($data);		//处理数
        if(!is_array($da)) return ['code'=>0,'msg'=>$da];
    	$result = $this->saveall($da);			//保存所有数据
    	if($result!==false){
    		return ['code'=>1,'mag'=>'保存成功'];
    	}else{
    		return ['code'=>0,'msg'=>'保存失败'];
    	}
    }
}